package com.app.micaihu.db;

import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.app.micaihu.configure.ConfigKeyConstant;
import com.app.micaihu.manager.AppConfigManager;

/**
 * Created by Guo on 2016/7/18.
 * 操作用户信息的dao
 */
public class UserDBDAO {
    private static UserDBDAO databaseDAO;

    private UserSQLiteOpenHelper userDB;

    SQLiteDatabase sqLiteDatabase = null;


    private UserDBDAO() {
    }

    public static UserDBDAO getInstance() {
        if (databaseDAO == null) {
            databaseDAO = new UserDBDAO();
        }
        return databaseDAO;
    }

    /**
     * 推送历史ID--以sp的形式存储
     * <p>
     * <p>
     * 无则写入并返回false，有则返回true
     *
     * @param
     */
    public static boolean ReceiverHistoryDB(String articleId) {
        StringBuilder stringBuilder = new StringBuilder();
        String localPushedId = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.news_pushed, "");

        if (!TextUtils.isEmpty(localPushedId)) {
            String[] pushs = localPushedId.split("_");
            if (pushs == null) {
                if (localPushedId.equals(articleId)) {
                    return true;
                } else {
                    stringBuilder.append(localPushedId).append("_").append(articleId);
                    AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.news_pushed, stringBuilder.toString());
                    return false;
                }
            } else {
                boolean ishas = false;
                int index = pushs.length - 10;
                if (index < 0) {
                    index = 0;
                }
                for (int i = 0; i < pushs.length; i++) {
                    if (TextUtils.isEmpty(pushs[i])) {
                        continue;
                    }
                    if (!ishas) {
                        if (pushs[i].equals(articleId)) {
                            ishas = true;
                        }
                    }
                    if (i >= index) {
                        stringBuilder.append(pushs[i]).append("_");
                    }
                }
                if (!ishas) {
                    stringBuilder.append(articleId);
                    AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.news_pushed, stringBuilder.toString());
                }
                return ishas;
            }
        }
        stringBuilder.append(articleId);
        AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.news_pushed, stringBuilder.toString());
        return false;
    }


}
